package chap09;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpReceive {
    public static  void main(String [] args){
        DatagramSocket ds=null;
        byte[] buf = new byte[1024];
        DatagramPacket dp=null;
        try{
            ds=new DatagramSocket(9000);
        }catch(SocketException ex){

        }//DatagramPacket的时候是byte 的数组
            dp=new DatagramPacket(buf,1024);//容器
        try{
            ds.receive(dp);//
        }catch(IOException e){
        e.printStackTrace();
        }

        //public String()  type=>string

        String str=new String(dp.getData(),0,dp.getLength())+"from"+dp.getAddress().getHostName()+":"+dp.getPort();
        //我还以为是句柄 其实不是
        System.out.println(str);
        ds.close();
    }
}
